package com.itmk.web.role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.web.role.entity.Role;
import com.itmk.web.role.entity.RoleParm;
import com.itmk.web.role.service.RoleService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import itmk.utils.ResultUtils;
import itmk.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 角色控制层
 */
@RestController
@RequestMapping("api/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询角色列表
     * @param parm
     * @return
     */
    @GetMapping("/list")
    public ResultVo list(RoleParm parm){
        IPage<Role> list=roleService.list(parm);
        return ResultUtils.success("查询成功",list);
    }


    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping
    public ResultVo addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if (save){
            return ResultUtils.success("新增角色成功");
        }
        return ResultUtils.error("新增角色失败");
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @PutMapping
    public ResultVo editRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        if (b){
            return ResultUtils.success("编辑角色成功");
        }
        return ResultUtils.error("编辑角色失败");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    public ResultVo deleteRole(@PathVariable("roleId") Long roleId){
        boolean b = roleService.removeById(roleId);
        if (b){
            return ResultUtils.success("删除角色成功");
        }
        return ResultUtils.error("删除角色失败");
    }


}
